package site.wetsion.employ.server.controller;

import java.math.BigInteger;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import site.wetsion.employ.server.pojo.ForumFollow;
import site.wetsion.employ.server.pojo.ForumPost;
import site.wetsion.employ.server.pojo.User;
import site.wetsion.employ.server.service.ForumService;
import site.wetsion.employ.server.util.Page;
import site.wetsion.employ.server.util.Result;

@Controller
public class ForumController {
	
	@Autowired
	private ForumService forumService;

//	@GetMapping("forum/list/{pageNum}")
//	public String forumList(@PathVariable int pageNum, HttpSession session, Model model){
//		User curr_user = (User) session.getAttribute("curr_user");
//		if(curr_user==null){
//			return "redirect:/login";
//		}
//		Page page = new Page(pageNum, 20);
//		List<ForumPost> forumPostList = forumService.getForumPostListByPage(page);
//		int count = forumService.getCountOfForumPost();
//		page.setTotalRecord(count);
//		model.addAttribute("forumPostList", forumPostList);
//		model.addAttribute("page", page);
//		return "forumlist";
//	}
	
	@GetMapping("forum/list/{pageNum}")
	public String forumList(@PathVariable int pageNum, String keyword, 
			HttpSession session, Model model){
		User curr_user = (User) session.getAttribute("curr_user");
		if(curr_user==null){
			return "redirect:/login";
		}
		Page page = new Page(pageNum, 20);
		List<ForumPost> forumPostList = null;
		int count = 0;
		if(keyword==null || "".equals(keyword)){
			forumPostList = forumService.getForumPostListByPage(page);
			count = forumService.getCountOfForumPost();
		}else{
			forumPostList = forumService.getForumPostListOnSearchByPage(page,keyword);
			count = forumService.getCountOfSearchForumPost(keyword);
		}
		page.setTotalRecord(count);
		model.addAttribute("forumPostList", forumPostList);
		model.addAttribute("page", page);
		model.addAttribute("keyword", keyword);
		return "forumlist";
	}
	
	/** 发布帖子*/
	@ResponseBody
	@PostMapping("forum/addForumPost")
	public Result addForumPost(ForumPost post, HttpSession session){
		User curr_user = (User) session.getAttribute("curr_user");
		if(curr_user==null){
			return new Result(505, "登录已失效，请重新登录");
		}
		post.setPost_starter_id(curr_user.getId());
		post.setPost_status(0);
		boolean rs = forumService.addForumPost(post);
		if(rs){
			return new Result(200, "发布成功");
		}
		return new Result(400, "发布失败");
	}
	
	@GetMapping("forum/detail/{id}/{pageNum}")
	public String forumDetail(@PathVariable("id") BigInteger id,
			@PathVariable("pageNum") int pageNum, Model model){
		Page page = new Page(pageNum, 15);
		ForumPost forumPost = forumService.getForumPostById(id);
		List<ForumFollow> followList = forumService.getForumPostDetail(page, id);
		Collections.sort(followList);
		System.out.println(followList.size());
		int count = forumService.getCountOfFollowOfPost(id);
		page.setTotalRecord(count);
		model.addAttribute("page", page);
		model.addAttribute("forumPost", forumPost);
		model.addAttribute("followList", followList);
		return "forumdetail";
	}
	
	/** 回复帖子*/
	@PostMapping("forum/addForumFollow")
	public String addForumFollow(ForumFollow follow, HttpSession session){
		User curr_user = (User) session.getAttribute("curr_user");
		if(curr_user==null){
			return "redirect:/login";
		}
		follow.setFollow_user_id(curr_user.getId());
		follow.setFollow_status(0);
		boolean rs = forumService.addForumFollow(follow);
		if(rs){
			return "redirect:/forum/detail/"+follow.getFollow_post_id()+"/1";
		}
		return "redirect:/forum/detail/"+follow.getFollow_post_id()+"/1";
		
	}
	
	/** 我的帖子列表*/
	@GetMapping("forum/mypostlist/{pageNum}")
	public String mypostlist(@PathVariable int pageNum, 
			Model model, HttpSession session){
		User user = (User) session.getAttribute("curr_user");
		if(user==null){
			return "redirect:/login";
		}
		Page page = new Page(pageNum, 15);
		List<ForumPost> forumPostList = forumService.getForumPostListOfUser(page, user.getId());
		int count = forumService.getPostCountOfUser(user.getId());
		page.setTotalRecord(count);
		model.addAttribute("mypostlist", forumPostList);
		model.addAttribute("page", page);
		return "mypostlist";
	}
	
	/** 回复我的消息列表*/
	@GetMapping("forum/mymessage/{pageNum}")
	public String messageList(@PathVariable int pageNum, HttpSession session, Model model){
		User user = (User) session.getAttribute("curr_user");
		if(user == null) {
			return "redirect:/login";
		}
		if(pageNum == 0){
			pageNum =1;
		}
		Page page = new Page(pageNum, 10);
		//获取所有消息
		List<ForumFollow> list=forumService.getAllFollowMessageOfUser(user.getId(), page);
		forumService.setLookMessageByUid(user.getId());
		
		model.addAttribute("messageList", list);
		model.addAttribute("page", page);
		return "forumMessage";
	}
	
	/** 获取查看的消息数*/
	@ResponseBody
	@GetMapping("forum/getUnLookMessageCount")
	public int getUncheckEnterpriseUserCount(HttpSession session){
		User user = (User) session.getAttribute("curr_user");
		return forumService.getUnLookMessageCount(user.getId());
	}
	
	@ResponseBody
	@GetMapping("forum/delPost")
	public Result delCampusNews(BigInteger postId){
		boolean rs = forumService.deleteForumPost(postId);
		if(rs){
			return new Result(200, "删除成功");
		}
		return new Result(400, "删除失败");
	}
	
}
